<?php
/**
 * 
 * @author tangbl
 * 
 */
class MemberAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 列表
	 */
	public function index() {
		
		if(IS_AJAX){
			import('ORG.Util.Page');
			$model     = D("MemberGroupView");
			
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);

			$map       = unescape($this->_get("where"));
			$map       = '1=1 '.$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			//echo $model->getLastSql();exit();
			
			$isactive2 = L('mb_form_ele_status_0');
			$isactive1 = L('mb_form_ele_status_1');
			$addr   = L('mb_form_address');
			$updt   = L('cfg_list_itf_btn_modify');
			$active = L('mb_list_active');
			$frozen = L('mb_list_frozen');
				
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				$data_mgroup    = '';		
				if($row['groupid']){
					$data_mgroup  = M("mgroup")->where('id='.$row['groupid'])->getField("title");
				}		
				
				if($row['isactive']==1){
					$isactive = $active;
					$html = 'class="btn icn-only red   delAlone" title="'.$frozen.'" filed="2"';
				}else{
					$isactive = $frozen;
					$html = 'class="btn icn-only green delAlone" title="'.$active.'" filed="1"';
				}
				$entry    = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'username' => $row['username'],
						'email'    => $row['email'],
						'groupid'  => $data_mgroup,
						'isactive' => $isactive,
						'addtime'  => date('Y-m-d',$row['addtime']),
						'deal'=>'
					<a href="'.__URL__.'/address/userid/'.$row['id'].'" class="btn blue icn-only" title="'.$addr.'"><i class="icon-book"></i></a> 
					<a href="'.__URL__.'/edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$updt.'"><i class="icon-edit"></i></a> 
					<a href="#" '.$html.' data-id="'.$row['id'].'"><i class="icon-key"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
			
			$model  = M("Mgroup");
			$groups = $model->select();
			$this->assign("mgroup",$groups);

			$this->display();
		}
	}
	
	/**
	 * 调整用户分组
	 */
	public function changeMgroup(){	
		
		$id   = $this->_get("id");
		$gid  = $this->_get("gid");
		
		if($id){
			$model = M('Memgroup');
			$id_arr = explode(',',$id);
			foreach($id_arr as $k=>$v){
				if($v){
					
					$map['userid']   = $v;
					//$map['groupid']  = $gid;
					$model->where($map)->delete();
					
					$data['userid']  = $v;
					$data['groupid'] = $gid;
					$data['addtime'] = time();		
					$data['addid']   = session(C("USER_AUTH_KEY"));
					$result   = $model->add($data);
				}
			}
		}
	}
	
	/**
	 * 添加
	 */
	public function add(){		
		if(IS_POST){
			$image = array("single"=>array("single"));
			parent::do_add("Member",$image);
		} else {				
			$this->assign("jumpUrl",U('/Member/add'));
			$this->display();
		}
	}
	
	//删除
	public function del(){
		
		$id  = $this->_get("id","intval",0);
		$act = $this->_get("act","intval",2);
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		$model = D("Member");
		$map['id'] = $id;
		$map['isactive'] = $act;
		
		$res = $model->save($map);
		if($res){
			$this->success(L("cfg_form_tipmsg_editsucc"));
		}else{
			$this->error(L("cfg_form_tipmsg_editfail"));
		}
	}
	
	//修改
	public function edit(){
		$member = D("Member");	
		if(IS_POST) {
			if($member->create()) {
				$solevar  = $member->solevar;
				$password = $this->_post("epassword");
				if($password != C("CONFUSE")){//修改密码
					$member->password = md5($password);	
				}
				
				add_edit_thumb("single",$solevar);
				
				$member->save();
				wlog(1,"",$member->id);
				
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$member->getError());
				$this->error($member->getError());
			}
		}else{
			$id  = $this->_get('id','intval',0);
			if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
			$data  = $member->find($id); 
			
			$single = find_thumb($data['solevar'],'single');
			$this->assign("single",$single);
			
			$this->assign("member",$data); 
			$this->display();
		}
	}
	
	/**
	 * 地址列表
	 */
	public function address(){
		
		if(IS_AJAX){
			
			$mid  = $this->_get("mid");
			import('ORG.Util.Page');
			$model     = D("Address");
			
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = '1=1 '.$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$type1 = L('mb_form_membertype1');
			$type2 = L('mb_form_membertype2');
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			$upd       = L('cfg_list_itf_btn_modify');
			$del       = L('cfg_list_itf_btn_delete');
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'        => $row['id'],
						'realname'  => $row['realname'],
						'mobile'    => $row['mobile'],
						'email'     => $row['email'],
						'address'   => $row['address'],
						'addtime'   => date('Y-m-d',$row['addtime']),
						'deal'=>' 
					 <a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
			
			$userid = $this->_get("userid","intval",0);
			$user   = M("Member")->find($userid);
			$this->assign("user",$user);
			$this->display();
		}	
	}
	
	/**
	 * 地址添加
	 */
	public function address_add(){
		
		$mid      = $this->_param('mid','intval',0);
		$address = D("Address");		
		if(IS_POST){
			if($address->create()){
				$address->user_id = $mid;	
				$address->add();
				wlog(1);
				$this->assign("jumpUrl",U('/Member/address/mid/'.$mid));
				$this->success(L('cfg_form_tipmsg_editsucc'));	
			}else{
				wlog(2,$address->getError());
				$this->error($address->getError());
			}
		} else {
			$this->assign("mid",$mid);			
			$this->assign("jumpUrl",U('/Member/address_add'));
			$this->display();
		}
	}
	
	//地址修改
	public function address_edit(){	
		$address = D("Address");
		$id  = $this->_param('id','intval',0);
		$mid = $this->_param('mid','intval',0);
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));	
		
		if(IS_POST){
			$url = array("url"=>'Member/address','param'=>'mid='.$mid);
			parent::do_edit("Address","","","",$url);
		} else {	
			$data  = $address->find($id); 
			
			$this->assign("address",$data); 
			$this->assign("id",$id);
			$this->assign("mid",$mid);
			$this->display();
		}
	}
	
	//删除
	public function address_del(){
		parent::do_del("Address");
	}
	
	/**
	 * 会员分组列表
	 */
	public function mgroup(){
		
		$model = M("mgroup");
		
		if(IS_AJAX){
			
		}else{
			$this->display();
		}
	}
	
	/**
	 * 添加会员分组
	 */
	public function mgroupAdd(){
		
		$model = M("mgroup");
		
		if(IS_POST){
			$param = array(
				'upload' => array("single"=>array("single")),
				'url'    => array('url'=>'Member/mgroup'),
			);
			parent::do_add("mgroup",$param);
		}else{
			$this->display();
		}
	}
	
	/**
	 * 修改分组列表
	 */
	public function mgroupEdit(){
		
		$model = M("mgroup");
		
		if(IS_POST){
			
		}else{
			$this->display();
		}
	}
	
	/**
	 * 删除分组列表
	 */
	public function mgroupDel(){
		parent::do_del("mgroup");
	}
	
	/**
	 * 会员等级列表
	 */
	public function mlevel(){
		
		$model = M("mlevel");
		
		if(IS_AJAX){
			
		}else{
			$this->display();
		}
	}
	
	/**
	 * 添加会员等级
	 */
	public function mlevelAdd(){
		
		$model = M("mlevel");
		
		if(IS_POST){
			
		}else{
			$this->display();
		}
	}
	
	/**
	 * 修改会员等级
	 */
	public function mlevelEdit(){
		
		$model = M("mlevel");
		
		if(IS_POST){
			
		}else{
			$this->display();
		}
	}
	
	/**
	 * 删除会员等级
	 */
	public function mlevelDel(){
		parent::do_del("mlevel");
	}
}
?>